package com.lianjia.sh.se.esb.server.log.dao;
import com.lianjia.sh.se.esb.server.log.model.MessageLogRepeat;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangjiangping
 * @since 2016-07-19 13:46
 */
@Repository
public class MessageLogRepeatDao {

    @Autowired
    private SqlSession sqlSession;


    /**
     * 插入日志
     * @param messageLogRepeat messageLogRepeat
     * @return
     */
    public Long insert(MessageLogRepeat messageLogRepeat) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("msgUUId", messageLogRepeat.getMsgUUId());
        paramMap.put("projectType", messageLogRepeat.getProjectType());
        paramMap.put("serviceType", messageLogRepeat.getServiceType());
        paramMap.put("data", messageLogRepeat.getData());
        paramMap.put("extendData", messageLogRepeat.getExtendData());
        paramMap.put("operatorEmpNo", messageLogRepeat.getOperatorEmpNo());
        paramMap.put("operatorIp", messageLogRepeat.getOperatorIp());
        paramMap.put("operateTime", messageLogRepeat.getOperateTime());
        paramMap.put("actionType", messageLogRepeat.getActionType());
        paramMap.put("errMaxRepeatCnt", messageLogRepeat.getErrMaxRepeatCnt());
        paramMap.put("errRepeatCnt", messageLogRepeat.getErrRepeatCnt());
        paramMap.put("status", messageLogRepeat.getStatus());
        sqlSession.insert("MessageLogRepeatMapper.insert", paramMap);
        return (Long) paramMap.get("id");
    }

    /**
     * 列新日志
     * @param id 主键
     * @param errRepeatCnt  已重发次数
     * @param status 状态
     * @return
     */
    public void update(Long id,
                       Integer errRepeatCnt,Integer status) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("errRepeatCnt", errRepeatCnt);
        paramMap.put("status", status);
        sqlSession.update("MessageLogRepeatMapper.update", paramMap);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    public MessageLogRepeat findById(Long id){
        return sqlSession.selectOne("MessageLogRepeatMapper.findById",id);
    }

    /**
     * 查询需要重发的消息日志
     * @param top
     * @return
     */
    public List<MessageLogRepeat> findTop(Integer top){
        return sqlSession.selectOne("MessageLogRepeatMapper.findTop",top);
    }
}
